package org.linhadig.swing.util;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

@SuppressWarnings("serial")
public class MaxLengthCurrencyDocument extends PlainDocument {
	private int maxChars;

	public MaxLengthCurrencyDocument() {
	}

	public MaxLengthCurrencyDocument(int maxChars) {
		this.maxChars = maxChars;
	}

	@Override
	public void insertString(int offs, String str, AttributeSet a)
			throws BadLocationException {
		if (str == null) return;
		
		if (",".equals(str) || ".".equals(str) ) {
			setMaxChars( getMaxChars()+1 );
		}
		
		if ((getLength() + str.length() < maxChars)) {
			super.insertString(offs, str, a);
		}
	}
	
	public int getMaxChars() {
		return maxChars;
	}

	public void setMaxChars(int maxChars) {
		this.maxChars = maxChars;
	}

}